//
//  HRSelectIconView.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSelectIconView.h"
#import "HRSelectIconCell.h"
@interface HRSelectIconView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *imageArray;

@end
@implementation HRSelectIconView

-(instancetype)initWithFrame:(CGRect)frame andSelectType:(NSString *)selectType
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"选择一个头像";
        titleLabel.textColor = TEXTCOLOR;
        titleLabel.font = CustomUIFont(15);
        [self addSubview:titleLabel];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(80, 130);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:layout];
        collectionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[HRSelectIconCell class] forCellWithReuseIdentifier:@"HRSelectIconCell"];
        [self addSubview:(self.collectionView = collectionView)];
        WeakSelf
        
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf);
            make.right.left.equalTo(weakSelf);
            make.height.mas_equalTo(20);
        }];
        
        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(20);
            make.left.right.bottom.equalTo(weakSelf);
        }];

        if ([selectType isEqualToString:@"男"]){
            selectType = @"man";
        }else{
            selectType = @"women";
        }
        self.imageArray = [NSMutableArray array];
        for (int i = 0; i < 13; i ++)
        {
            [self.imageArray addObject:[NSString stringWithFormat:@"%@_%d",selectType,i+1]];
        }
        
      
    }
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return [collectionView dequeueReusableCellWithReuseIdentifier:@"HRSelectIconCell" forIndexPath:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
   
    [(HRSelectIconCell *)cell configureCellWithPostURL:self.imageArray[indexPath.row]];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectIconBlock)
    {
        self.selectIconBlock(self.imageArray[indexPath.row]);
    }
}






@end
